<?php
require_once('TML_Clases.php');
require_once('PHPTemplateView.inc');	

//-----------------------------------------------------------------------------	
final class VFS_FileClass
{
	var $name  = '';
	var $type  = 'file';
	var $title = '';
	var $VFS_atributes;

	private $id=0;
	private $file_data;
	private $m_templateName;
	private $sqlquerytempl;

	private $atributes=0;
	private $m_view = NULL;
  
	private $fs;
	private $path;

	//----------------------------------------------------------------------------
	//установить получить метаинформацию
	public function meta($meta = null)
	{
		$retval = array();

		if(!$meta)
		{
			$sql_query = sprintf('select * from '.VFS_FILES_META.' where file_id=%u', $this->id);
			$result    = mysql_query($sql_query);

			while($row = mysql_fetch_assoc($result))
			{
				$retval[$row['name']] = $row['value'];
			};
		}
		else
		{
			$sql_query = sprintf('delete from '.VFS_FILES_META.' where file_id=%u', $this->id);
			mysql_query($sql_query);

			foreach($meta as $name => $value)
			{
				$sql_query = sprintf('insert into '.VFS_FILES_META.' set file_id=%u, name="%s", value="%s"', $this->id, $name, $value);
				mysql_query($sql_query);
			};
		};

		return $retval;
	}

	public function GetInfo()
	{
		return $this->file_data;
	}

	public function Modify($params)
	{
		if($this->fs instanceof I_VFSCreater)
		{
			return $this->fs->modify($this->path, $params);
		}
		else
		{
			return ACCESS_DENIED;
		};
	}

	public function Delete()
	{
		if($this->fs instanceof I_VFSCreater)
		{
			return $this->fs->delete($this->path);
		}
		else
		{
			return ACCESS_DENIED;
		};
	}

	//----------------------------------------------------------------------------
	//                      GetContent
	//----------------------------------------------------------------------------
	private function __GetContainerStruct($data)
	{
	  $return=array('rows'=>array(),'head'=>'', 'footter'=>'', 'separators'=>array());
	  $data=preg_replace('/<table[^<>]*?'.'>/si','',$data);
	  $data=preg_replace('/<\/table[^<>]*>/si','',$data);

		$data=preg_replace('/<tr[^<>]*?'.'>/si','',$data);
	  $data=preg_replace('/<\/tr[^<>]*>/si','',$data);

	  $data=preg_replace('/<td[^<>]*?'.'>/si','',$data);
	  $data=preg_replace('/<\/td[^<>]*>/si','',$data);

	  preg_match('/<head[^<>]*?'.'>.*?<\/head[^<>]*>/si',$data,$head);
	  $return['head']=preg_replace('/<head[^<>]*?'.'>/si','',$head[0]);
	  $return['head']=trim(preg_replace('/<\/head[^<>]*?'.'>/si','',$return['head']));
	  $data=preg_replace('/<head[^<>]*?'.'>.*?<\/head[^<>]*>/si','',$data);
	  preg_match('/<footter[^<>]*?'.'>.*?<\/footter[^<>]*>/si',$data,$footter);
	  $return['footter']=preg_replace('/<footter[^<>]*?'.'>/si','',$footter[0]);
	  $return['footter']=trim(preg_replace('/<\/footter[^<>]*?'.'>/si','',$return['footter']));
	  $data=preg_replace('/<footter[^<>]*?'.'>.*?<\/footter[^<>]*>/si','',$data);

	  preg_match_all('/<separator[^<>]*?'.'>.*?<\/separator[^<>]*>/si',$data,$separators);
	  $data=preg_replace("/<separator[^<>]*?".">.*?<\/separator[^<>]*>/si",'',$data);

	  foreach($separators[0] as $separator)
	  {
	    $separator=preg_replace('/<separator[^<>]*?'.'>/','',$separator);
	    $separator=trim(preg_replace('/<\/separator[^<>]*?'.'>/','',$separator));
	    array_push($return['separators'],$separator);
	  };

	  preg_match_all('/<row[^<>]*?'.'>.*?<\/row[^<>]*>/si',$data,$rows);
	  $ContParts=preg_split('/<row[^<>]*?'.'>.*?<\/row[^<>]*>/si',$data);

	  for($i=0,$k=0;$k<count($ContParts);$k++,$i+=2)
	  {
	    if(trim($ContParts[$k]))
	    {
	      $return['rows'][$i][0]=$ContParts[$k];
	      $return['rows'][$i][1]=false;
	    }
	    else
	      $i--;

	    if(@$rows[0][$k])
	    {
	      $return['rows'][$i+1][0]=preg_replace('/<row[^<>]*?'.'>/','',$rows[0][$k]);
	      $return['rows'][$i+1][0]=trim(preg_replace('/<\/row[^<>]*?'.'>/','',$return['rows'][$i+1][0]));
	      $return['rows'][$i+1][1]=true;
	    };
	  };

	  return $return;
	}
	
	private function __CompileContainer($container)
	{
	  $inserts=array();//массив вставок
	  preg_match_all('/<templinsert[^<>]*?'.'>.*?<\/templinsert[^<>]*>/si',$container,$inserts);
	  $MainTemplParts=preg_split('/<templinsert[^<>]*?'.'>.*?<\/templinsert[^<>]*>/si',$container);
	  $InsertsParser=new TML_InsertsParserForTemplCompiler();

	  for($i=0,@$buff.=$MainTemplParts[0];$i<count($inserts[0]);$i++)
	  {
	    $InsertsParser->new_element($inserts[0][$i]);
	    ($InsertsParser->generate($ref,$skip,$error))||die($error);
	    $buff.= sprintf('<?php $this->getcontent(\'%s\');?>', $ref).$MainTemplParts[$i+1];
	  };

	  $InsertsParser->destroy();

	  return $buff;
	}

	public function GetContent($cid)
	{
		switch($cid)
		{
			case '0.metakeywords':
				$cid = 'metakeywords';
			break;

			case '0.metadescription':
				$cid = 'metadiscr';
			break;

			case '0.header':
				$cid = 'header';
			break;
	  };		

		$sql_query = sprintf($this->sqlquerytempl, $this->id, $cid);
		$row       = mysql_fetch_assoc(mysql_query($sql_query));

		switch($row['content_type'])
		{
			case 'container':
			case 'acontainer':
				$tmpFileName = TMPPATH.md5(sprintf('%s_%s', $_SERVER['REQUEST_URI'], $cid));

				if(!file_exists($tmpFileName))
				{
					if($row['content_type']=='container')
					{
						$ContainerStruct=$this->__GetContainerStruct($row['content']);

						for($buff=$ContainerStruct['head'],$i=0;$i<count($ContainerStruct['rows']);$i++)
						{
							$buff.=$ContainerStruct['rows'][$i][0];
						};

						$buff.=$ContainerStruct['footter'];
					}
					else
					{
						$buff=$row['content'];
					};

					$buff = $this->__CompileContainer($buff);
					$fp   = fopen($tmpFileName, 'w');
					fwrite($fp,$buff);
					fclose($fp);
				};

				include($tmpFileName);
			break;

			case 'component':
				eval($row['content']);
			break;

			default:
				switch ($cid)
				{
					case 'header':
						$rows = explode("\n", $row['content']);

						foreach($rows as $key => $row)
						{
							header(trim($row));
						};
					break;

					default:
						echo $row['content'];
				};
		};
	}

	//----------------------------------------------------------------------------
	public function GetView()
	{	
		if(!$this->m_view)
		{
			$this->m_view  = new PHPTemplateView(TEMPLDIRPATH, $this->m_templateName, $this);
		};

		return $this->m_view;
	}

	public function VFS_FileClass($params)
	{
		$this->name  = $params['name'];
		$this->title = $params['title'];
		$this->id    = $params['id'];

		$this->m_templateName  = $params['template'];
		$this->file_data       = $params;
		$this->sqlquerytempl   = 'select * from '.VFS_FILES_CONTENT.' where (file_id=%u) && (file_content_id="%s")';

		$this->VFS_atributes  = ($this->atributes&16)?1:0;
		$this->VFS_atributes |= ($this->atributes&64)?2:0;
		$this->VFS_atributes |= ($this->atributes&256)?4:0;

		$this->fs   = $params['fs'];
		$this->path = $params['path'];
	}
};
?>